package com.example.weatherserctary2;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

@SuppressLint("ShowToast")
public class AddCityActivity extends Activity implements OnClickListener{
	private Button btn;
	private Dialog dialog;
	private EditText edit;
	private SharedPreferences sp;
	private SharedPreferences.Editor editor;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add);
		
		sp = getSharedPreferences("city_info", MODE_PRIVATE);
		editor = sp.edit();
		btn = (Button)findViewById(R.id.btn_add);
		btn.setOnClickListener(this);
		edit = new EditText(this);
		dialog = new AlertDialog.Builder(this)
		.setTitle("请输入")
		.setView(edit)
		.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				String city = edit.getText().toString();  //新的值
				Log.d("输入", city);
				String citys = sp.getString("citys", "");  //原有的值 
				if(!citys.contains(city)){
					editor.putString("citys", citys+" "+city);
					editor.commit(); //提交
					System.out.println(".....文件中已保存了的城市：");
					Toast.makeText(getApplicationContext(), "文件中已保存了的城市", Toast.LENGTH_SHORT);
					System.out.println(citys);
				}
				Intent intent = new Intent(AddCityActivity.this,MainActivity.class);
				startActivity(intent);
				finish();
			}

		})
		.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();  //隐藏
			}

		})
		.create();  //创建对话框
	}

	@Override
	public void onClick(View arg0) {
		//edit.setText("");
		dialog.show();  //显示对话框
	}
}
